package com.longlong.backend.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author longlong
 * @date 2025/1/26
 */
public class MySpring {
    /**
     * 存储对象
     */
    private static final Map<String, Object> beanMap = new HashMap<>();

    /**
     * 获取对象
     * 单例模式
     *
     * @param className 类名
     * @param <T>       泛型
     * @return 对象
     */
    public static synchronized <T> T getBean(String className) {
        T obj = (T) beanMap.get(className);
        if (obj == null) {
            try {
                obj = (T) Class.forName(className).newInstance();
                beanMap.put(className, obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return obj;
    }
}
